/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cnam.mobileshopping.ws;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.cnam.mobileShopping.entity.User;
import org.cnam.mobileshopping.ejb.user.UserLocal;

/**
 * REST Web Service
 *
 * @author Administrateur
 */
@Stateless
@Path("user")
public class UserResource {

    @EJB(mappedName="ejb/mobileshopping/user")
    UserLocal userLocal;
        

    /** Creates a new instance of GenericResource */
    public UserResource() {
    }

    
    @GET
    @Path("auth")
    @Produces("application/json")
    public Response login(  @QueryParam("login") String login, 
                            @QueryParam("password") String password) {
        User user = userLocal.getUserByLoginAndPassword(login, password);
        if (user != null) {
            return Response.ok(user).build();
        } else {
            if (login == null) {
                return Response.status(Response.Status.UNAUTHORIZED).
                        entity("login is null").build();
            }
            if (password == null) {
                return Response.status(Response.Status.UNAUTHORIZED).
                        entity("password is null").build();
            }
            return Response.status(Response.Status.UNAUTHORIZED).build();
        }
    }
    
    @GET
    @Path("creerUtilisateur")
    @Produces("application/json")
    public Response creerUtilisateur(@QueryParam("pseudo") String pseudo,  
            @QueryParam("login") String login, @QueryParam("password") String password)
    {
        return Response.ok(userLocal.addUser(pseudo, login, password)).build();
    }
    
    @GET
    @Path("getUsers")
    @Produces("application/json")
    public List<User> getUsers()
    {
        return userLocal.getUsers();
    }
    
}
